#include <allegro.h>
#include <stdio.h>
#include <string.h>
#include"fonctions.h"

void load_grace(Personnage *p) // chargement des images
{
	int i;
	char Buffer[50];
	for (i = 0; i < NB_IMAGES; i++) 
	{
		sprintf(Buffer, "./Pictures/right0%d.bmp", i);
		p->img_right[i] = load_bitmap(Buffer, NULL);
		sprintf(Buffer, "./Pictures/left0%d.bmp", i);
		p->img_left[i] = load_bitmap(Buffer, NULL);
	}
}


void destroy_grace(Personnage *p) //effacer les images
{
	int i;
	for (i = 0; i < NB_IMAGES; i++)
	{
		destroy_bitmap(p->img_right[i]);
		destroy_bitmap(p->img_left[i]);
	}

}

void init_grace(Personnage *p)
{
	p->direction = 0;
	p->x = 2;
	p->y = 100;
}

void move_grace_right(Personnage *p,int *compteur,BITMAP * Background, int *j,int * move)
{   
   variable v;


if((*j)>Background->w-300)
v.x=p->x;
else
v.x=(*j);
   
   
   v.y=p->y;
   v.w=p->img_right[0]->w;
   v.h=p->img_right[0]->h;
	




   if (key[KEY_RIGHT] && (p->x < SCREEN_W - p->img_right[0]->w) && !collision_background_right( v, Background,0)  ) 
    {




if(((*j)>= 200)&&((*j)<=600))
 {
 p->direction = 0;
  if((p->y<200)&&((*j)<400))
    {
 p->direction=0;
      p->y=p->y-3;
     
    }

if(p->y<300){
  if((*j)>490)
  {
   p->y=p->y+2;
   //p.dir=0;
  }}

 }

	if((*j)<Background->w-SCREEN_W)
             (*j)=(*j)+3;	
        
        /* if( (p->x >300) && ( (*j)<Background->w -SCREEN_W) )
                 p->x=300;
         */ 
          if( (*j)>=Background->w-SCREEN_W)
              {
                p->x=p->x++;
               }  
         
  *compteur+=1;
	   // (p->x)+=1;
	    p->direction = 0; //right
 (*move)=1;
    }
else
 (*move)=0;
}

void move_grace_left(Personnage *p,int *compteur,BITMAP * Background,int j)
{
   variable v;
  
if((j)>Background->w-300)
v.x=p->x;
else
v.x=(j);
   
//v.x=(j);

   v.y=p->y;
   v.w=p->img_right[0]->w;
   v.h=p->img_right[0]->h;
	


if (key[KEY_LEFT] && (p->x > 0 + p->img_left[0]->w )&& !collision_background_left( v, Background,0) ) // 
{
		*compteur+=1;   

		(p->x)-=1;
		p->direction = 1; //left
	}
}

void jump_grace(Personnage *p,BITMAP * Background,int j,int * test)
{
   variable v;
   

if( j>Background->w-SCREEN_W)
  v.x=p->x;
else
   v.x=j;
   
   v.y=p->y;
   v.w=p->img_right[0]->w;
   v.h=p->img_right[0]->h;


if(p->y == 10) 
{
    (*test)=0;
}


if((*test)==1)
{

if (key[KEY_UP] && (p->y >= 10 )&& !collision_background_up(v,Background,0))  // test sur la hauteur du Jump
{
		(p->y)-=10; // monter le personnage avec 6 degré
		 p->direction = 2; // changer la direction pour jump
}

}

if(collision_background_down(v,Background,0) )
{  (*test)=1;}


}

void gravity_grace(Personnage *p,BITMAP * Background,int j)
{ //while (p->y>SCREEN_H-100){
   variable v;
   
if((j)>Background->w-300)
v.x=p->x;
else
  v.x=j;
   
   v.y=p->y;
   v.w=p->img_right[0]->w;
   v.h=p->img_right[0]->h;


       if(v.y<=SCREEN_H && !collision_background_down( v, Background,0) ) // test si le personnage arriver a la fin du screen : bas
            p->y=p->y+4;
}  

void move_grace(Personnage *p,int *compteur,BITMAP * Background, int *j,int *move,int *test)  // la fonction permettant le movement du personange
{
	move_grace_right(p,compteur,Background,j,move);
	move_grace_left(p,compteur,Background,*j);
	jump_grace(p,Background,*j,test);
	gravity_grace(p,Background,*j);
}

void draw_grace(Personnage p,BITMAP *Buffer, int compteur)
{
	static int last_direction;

	if (p.direction == 0){ //moving right

		draw_sprite(Buffer, p.img_right[compteur % (NB_IMAGES-1) ],p.x, p.y);
		last_direction=0;
	}

	if(p.direction == 1){ //moving left

		draw_sprite(Buffer, p.img_left[compteur % (NB_IMAGES-1) ],p.x, p.y);
		last_direction=1;
	}
	if (p.direction == 2 && last_direction==0) {	//jumping right

		draw_sprite(Buffer, p.img_right[NB_IMAGES - 1],p.x, p.y);
	}
	if (p.direction == 2 && last_direction==1) {	//jumping left

		draw_sprite(Buffer, p.img_left[NB_IMAGES - 1],p.x, p.y);
	}
}

BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);
 }


void draw_background(BITMAP *Background,BITMAP*Buffer,int j)
{
	blit(Background, Buffer, j, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}


int collision_background_right(variable v,BITMAP* BackgroundCol,int o)
{


   int x=v.x+ v.w;
   int y=v.y + (v.h /2);


 if(get_pixel(BackgroundCol, x,y)==o){

return 1;
}
else{
return 0;
}

}

int collision_background_left(variable v,BITMAP* BackgroundCol,int o){

    
   int x=v.x;
   int y=v.y + (v.h /2);



 if(get_pixel(BackgroundCol, x,y)==o){

return 1;
}
else{
return 0;
}

}

int collision_background_up(variable v,BITMAP* BackgroundCol,int o)
{
   
   int x=v.x+ (v.w /2);
   int y=v.y ;


if(get_pixel(BackgroundCol, x,y)==o){

return 1;
}
else{
return 0;
}
}

int collision_background_down(variable v,BITMAP* BackgroundCol,int o)
{   
   int x=v.x+ (v.w /2);
   int y=v.y + v.h;



if(get_pixel(BackgroundCol, x,y)==o){

return 1;
}
else{
return 0;
}
}

	
int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}

int collision(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2)
{
if((x1 > x2 + w2-1)|| (y1> y2+ h2-1) || (x2 > x1+ w1-1)||(y2 > y1 + h1-1))
     return 0;

return 1;
}

void soundm()
{

SAMPLE * sound;

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) 
{
allegro_message("Sound Error: %s", allegro_error);
allegro_exit();
//return 1;
}

sound = load_wav("./SureShot.wav");
if (!sound)
{
allegro_message("Error reading wav file");
allegro_exit();
//return 1;
}

play_sample(sound, 255, 100,1000, 2);
set_volume(255, 105); 
}

